package com.lanhe.admin.controller.identifier;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lanhe.common.annotation.Log;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import com.lanhe.identifier.domain.LhIdentifier;
import com.lanhe.identifier.service.ILhIdentifierService;


/**
 * 识别码(LhIdentifier)表控制层
 *
 * @author makejava
 * @since 2023-09-18 20:07:29
 */
@RestController
@RequestMapping("lhIdentifier")
public class LhIdentifierController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private ILhIdentifierService lhIdentifierService;

    /**
     * 分页查询所有数据
     */
    @PreAuthorize("@ss.hasPermi('identifier:identifier:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhIdentifier identifier) {
        startPage();
        List<LhIdentifier> list = lhIdentifierService.selectIdentifierList(identifier);
        return getDataTable(list);
    }

    /**
     * 根据编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('identifier:identifier:query')")
    @GetMapping(value = { "/", "/{identifierId}" })
    public AjaxResult getInfo(@PathVariable(value = "identifierId") Long identifierId)
    {
        return success(lhIdentifierService.selectIdentifierById(identifierId));
    }

    /**
     * 生成识别码
     */
    @PreAuthorize("@ss.hasPermi('teacher:identifier:add')")
    @Log(title = "识别码管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LhIdentifier identifier)
    {
        if (null == identifier.getNumber()  ||  identifier.getNumber() <=0)
        {
            return error("请输入要生成的数量");
        }
        if (identifier.getNumber()> Constants.GeneralIntType.TEN_THOUSAND){
            return error("生成的数量不能大于10000条");
        }
        List<LhIdentifier> lhIdentifiers = new ArrayList<>();
        Date nowDate = new Date();
        for (int i = 0; i < identifier.getNumber(); i++) {
            LhIdentifier lhIdentifier = new LhIdentifier();
            lhIdentifier.setIdentifier(UUID.randomUUID().toString().replaceAll("-", ""));
            lhIdentifier.setCreateBy(getUsername());
            lhIdentifier.setCreateTime(nowDate);
            lhIdentifiers.add(lhIdentifier);
        }
        return toAjax(lhIdentifierService.insertIdentifier(lhIdentifiers));
    }

    /**
     * 删除识别码
     */
    @PreAuthorize("@ss.hasPermi('identifier:identifier:remove')")
    @Log(title = "识别码管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{identifierIds}")
    public AjaxResult remove(@PathVariable Long[] identifierIds)
    {
        if (ObjectUtils.isNotEmpty(lhIdentifierService.selectIdentifierByIds(identifierIds))){
            return error("不能删除已经提取的识别码");
        }
        return toAjax(lhIdentifierService.deleteIdentifierByIds(identifierIds));
    }

    /**
     * 统计已提取、未提取
     */
    @Log(title = "识别码管理", businessType = BusinessType.DELETE)
    @GetMapping("/stat")
    public AjaxResult stat()
    {
        return success(lhIdentifierService.stat());
    }

}
